<script setup>
import { ref } from 'vue'
import { artListService } from '@/api/article'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from '@/views/article/components/ChannelSelect.vue'
import { formatDate } from '@/utils/format'
import ArticleEdit from '@/views/article/components/ArticleEdit.vue'
import { artDelService, artDetailService } from '@/api/article'

const form = ref({
  pagenum: 1,
  pagesize: 10,
  cate_id: '',
  state: ''
})

//添加loding状态
const loading = ref(false)

const tableData = ref([])
const tableCount = ref({
  total: 0
})

const getArticleList = async () => {
  loading.value = true
  const res = await artListService(form.value)
  if(res.status === 200){
    tableData.value = res.data.data
    tableCount.value.total = res.data.total
  }
  loading.value = false
}
getArticleList()

//处理分页逻辑
const onSizeChange = (val) => {
  form.value.pagesize = val
  form.value.pagenum = 1
  getArticleList()
}
//处理当前页逻辑
const onCurrentChange = (val) => {
  form.value.pagenum = val
  getArticleList()
}

//重置表单
const resetForm = () => {
  form.value = {
    pagenum: 1,
    pagesize: 10,
    cate_id: '',
    state: ''
  }
  getArticleList()
}

const articleEditRef = ref(null)

//添加文章
const onAddArticle = () => {
  articleEditRef.value.open({})
}

const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}

//删除文章
const onDelArticle = async (row) => {
  ElMessageBox.confirm('确认删除该文章吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    const res = await artDelService(row.id)
    if(res.status === 200){s
      ElMessage.success('删除文章成功')
      getArticleList()
    }
  })
}

//查看文章详情
const onDetailArticle = async (row) => {
  const res = await artDetailService(row.id)
  console.log(res)
  if(res.status === 200){
    articleEditRef.value.open(res.data.data)
  }
}
</script>
<template>
  <PageContainer title="文章管理">
    <template #extra>
      <el-button @click="onAddArticle">添加文章</el-button>
    </template>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="文章分类:">
        <ChannelSelect v-model="form.cate_id"></ChannelSelect>
      </el-form-item>
      <el-form-item label="文章状态:">
        <el-select v-model="form.state" placeholder="请选择">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getArticleList">搜索</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>

  <el-table :data="tableData" style="width: 100%" v-loading="loading">
    <el-table-column prop="title" label="文章标题" width="180">
    <template #default="{row}">
      <p @click="onDetailArticle(row)">{{ row.title }}</p>
    </template>
    </el-table-column>
    <el-table-column prop="cate_name" label="分类" width="180" />
    <el-table-column label="发表时间" prop="pub_date">
      <template #default="{row}">
        {{ formatDate(row.pub_date) }}
      </template>
    </el-table-column>
    <el-table-column prop="state" label="状态" width="180" />
    <el-table-column label="操作" width="150" >
      <template #default="{row}">
        <el-button type="primary" :icon="Edit" circle plain @click="onEditArticle(row)"></el-button>
        <el-button type="danger" :icon="Delete" circle plain @click="onDelArticle(row)"></el-button>
      </template>
    </el-table-column>
  </el-table>

    <el-pagination
      v-model:current-page="form.pagenum"
      v-model:page-size="form.pagesize"
      :page-sizes="[2, 3, 5, 10]"
      :background="true"
      layout=" jumper, total, sizes, prev, pager, next"
      :total="tableCount.total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin: 20px;justify-self: end;"
    />

    <ArticleEdit ref="articleEditRef"></ArticleEdit>
  </PageContainer>
    </template>
<style>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
.example-pagination-block + .example-pagination-block {
  margin-top: 10px;
}
</style>
